﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;

using CE.User;
using CE.Global;
using CE.Database;
using CE.Quotes;

public partial class management_User_quotesummary_report : System.Web.UI.Page
{
    string PageCode = "CXTHRQSR";
    DataSet _dsSummary = new DataSet();
    DataTable _dtCISummary = new DataTable();
    DataTable _dtPASummary = new DataTable();
    DataTable _dtTISummary = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/ManagementSecureLogin.aspx");
        }
        if (!IsPostBack)
        {
            lblError.Text = "";
            ddlDay1.Visible = true;
            lblFromDate.Text = "Date :";
            lblToDate.Visible = false;
            ddlDay2.Visible = false;
            ddlMonth2.Visible = false;
            ddlYear2.Visible = false;
            ddlYear1.Items.Clear();
            ddlYear2.Items.Clear();

            int _cryr = CE.Global.General.GetUTCDateTime().Year;
            int _styr = 2010;
            for (int _year = _cryr; _year >= _styr; _year--)
            {
                ddlYear1.Items.Add(new ListItem(_year.ToString(), _year.ToString()));
                ddlYear2.Items.Add(new ListItem(_year.ToString(), _year.ToString()));
            }

            ddlDay1.Text = CE.Global.General.GetUTCDateTime().Day.ToString("00");
            ddlMonth1.Text = CE.Global.General.GetUTCDateTime().Month.ToString("00");
            ddlYear1.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000");
            ddlDay2.Text = CE.Global.General.GetUTCDateTime().Day.ToString("00");
            ddlMonth2.Text = CE.Global.General.GetUTCDateTime().Month.ToString("00");
            ddlYear2.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000");

            lblCITotalCount.Text = "";
            lblCINewCount.Text = "";
            lblCIExistCount.Text = "";
            lblCICount.Visible = false;
            lblCIRecordCount.Text = "";
            divCI.Visible = false;
        }
        if (Convert.ToInt64(Session[General.UserId].ToString()) != 1)
        {
            CheckPageAccess();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Cache.Remove("CIResult");
        ViewState["CISortExpression"] = "QuoteDate";
        ViewState["CISortOrder"] = " ASC";

        LoadGrid();
    }

    protected void ddlReportType_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblError.Text = "";
        ddlDay1.Text = CE.Global.General.GetUTCDateTime().Day.ToString("00");
        ddlDay2.Text = CE.Global.General.GetUTCDateTime().Day.ToString("00");
        ddlMonth1.Text = CE.Global.General.GetUTCDateTime().Month.ToString("00");
        ddlMonth2.Text = CE.Global.General.GetUTCDateTime().Month.ToString("00");
        ddlYear1.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000");
        ddlYear2.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000");
        if (Convert.ToInt16(ddlReportType.Text) == 0)
        {
            ddlDay1.Visible = false;
            lblFromDate.Text = "Date :";
            lblToDate.Visible = false;
            ddlDay2.Visible = false;
            ddlMonth2.Visible = false;
            ddlYear2.Visible = false;
        }
        else if (Convert.ToInt16(ddlReportType.Text) == 1)
        {
            ddlDay1.Visible = true;
            lblFromDate.Text = "Date :";
            lblToDate.Visible = false;
            ddlDay2.Visible = false;
            ddlMonth2.Visible = false;
            ddlYear2.Visible = false;
        }
        else if (Convert.ToInt16(ddlReportType.Text) == 2)
        {
            lblFromDate.Text = "From Date :";
            lblToDate.Text = " To Date :";
            lblToDate.Visible = true;
            ddlDay2.Visible = true;
            ddlMonth2.Visible = true;
            ddlYear2.Visible = true;
            ddlDay1.Visible = true;
        }
    }

    protected void grvCIQuotesSummary_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int _Count = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "RecordCount"));
            int _NCount = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "NewUserCount"));
            int _RecordCount = Convert.ToInt32(lblCITotalCount.Text);
            _RecordCount = _RecordCount + _Count;
            lblCITotalCount.Text = _RecordCount.ToString();
            int _NewCount = Convert.ToInt32(lblCINewCount.Text);
            _NewCount = _NewCount + _NCount;
            lblCINewCount.Text = _NewCount.ToString();
            lblCIExistCount.Text = (_RecordCount - _NewCount).ToString();
        }
    }

    public bool ValidDate(Int32 Year, Int32 Month, Int32 Day)
    {
        bool _isValid = true;
        int daysInMonth = 0;
        string leap, monthValid, dayValid, yearValid;
        if (Year >= 0000)
        {
            yearValid = "yes";
        }
        else
        {
            yearValid = "no";
        }
        if (Year % 400 == 0)
        {
            leap = "yes";
        }
        else if (Year % 100 == 0)
        {
            leap = "no";
        }
        else if (Year % 4 == 0)
        {
            leap = "yes";
        }
        else
        {
            leap = "no";
        }

        switch (Month)
        {
            case 1: daysInMonth = 31;
                break;
            case 3: daysInMonth = 31;
                break;
            case 4: daysInMonth = 30;
                break;
            case 5: daysInMonth = 31;
                break;
            case 6: daysInMonth = 30;
                break;
            case 7: daysInMonth = 31;
                break;
            case 8: daysInMonth = 31;
                break;
            case 9: daysInMonth = 30;
                break;
            case 10: daysInMonth = 31;
                break;
            case 11: daysInMonth = 30;
                break;
            case 12: daysInMonth = 31;
                break;
        }

        if (Month >= 1 && Month <= 12)
        {
            monthValid = "yes";
        }
        else
        {
            monthValid = "no";
        }
        if (Month == 2)
        {
            if (leap == "no")
            {
                daysInMonth = 28;
            }
            else if (leap == "yes")
            {
                daysInMonth = 29;
            }
        }

        if (1 <= Day && Day <= daysInMonth)
        {
            dayValid = "yes";
        }

        else
        {
            dayValid = "no";
        }

        if ((dayValid == "yes") && (monthValid == "yes") && (yearValid == "yes"))
        {
            _isValid = true;
        }
        else
        {
            _isValid = false;
        }

        return _isValid;
    }

    public bool Validatioin()
    {
        lblError.Text = "";
        bool _isValid = true;
        DateTime _fromDate, _toDate;
        if (Convert.ToInt16(ddlReportType.Text) == 1)
        {
            if (ddlDay1.SelectedIndex >= 0)
            {
                if (ValidDate(Convert.ToInt32(ddlYear1.Text), Convert.ToInt32(ddlMonth1.Text), Convert.ToInt32(ddlDay1.Text)))
                {
                    _fromDate = Convert.ToDateTime(Convert.ToInt32(ddlYear1.Text) + "/" + Convert.ToInt32(ddlMonth1.Text) + "/" + Convert.ToInt32(ddlDay1.Text));
                    if (_fromDate > CE.Global.General.GetUTCDateTime())
                    {
                        _isValid = false;
                        lblError.Text = "Date should be less than or equal to Today";
                    }
                }
                else
                {
                    _isValid = false;
                    lblError.Text = "Select Valid Date";
                }
            }
            else
            {
                _isValid = false;
                lblError.Text = "Select Date";
            }
        }
        else if (Convert.ToInt16(ddlReportType.Text) == 2)
        {
            if (ddlDay1.SelectedIndex >= 0 && ddlDay2.SelectedIndex >= 0)
            {
                if (ValidDate(Convert.ToInt32(ddlYear1.Text), Convert.ToInt32(ddlMonth1.Text), Convert.ToInt32(ddlDay1.Text)))
                {
                    _fromDate = Convert.ToDateTime(Convert.ToInt32(ddlYear1.Text) + "/" + Convert.ToInt32(ddlMonth1.Text) + "/" + Convert.ToInt32(ddlDay1.Text));
                    if (ValidDate(Convert.ToInt32(ddlYear2.Text), Convert.ToInt32(ddlMonth2.Text), Convert.ToInt32(ddlDay2.Text)))
                    {
                        _toDate = Convert.ToDateTime(Convert.ToInt32(ddlYear2.Text) + "/" + Convert.ToInt32(ddlMonth2.Text) + "/" + Convert.ToInt32(ddlDay2.Text));
                        if (_fromDate <= CE.Global.General.GetUTCDateTime())
                        {
                            if (_fromDate > _toDate)
                            {
                                _isValid = false;
                                lblError.Text = "From date should be less than To date";
                            }
                        }
                        else
                        {
                            _isValid = false;
                            lblError.Text = "From date should be less than Today";
                        }
                    }
                    else
                    {
                        _isValid = false;
                        lblError.Text = "Select Valid To date";
                    }
                }
                else
                {
                    _isValid = false;
                    lblError.Text = "Select Valid From date";
                }
            }
            else
            {
                _isValid = false;
                lblError.Text = "Select From date and To date";
            }
        }
        return _isValid;
    }

    public void LoadGrid()
    {
        lblError.Text = "";
        if (Validatioin() == true)
        {
            txtReportDate1.Text = "";
            txtReportDate2.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000") + CE.Global.General.GetUTCDateTime().Month.ToString("00") + CE.Global.General.GetUTCDateTime().Day.ToString("00");
            txtReportDate1.Text = ddlYear1.Text;
            txtReportDate1.Text += ddlMonth1.Text;
            if (ddlReportType.SelectedIndex > 0)
            {
                if (ddlDay1.SelectedIndex >= 0)
                {
                    txtReportDate1.Text += ddlDay1.Text;
                }
            }
            if (ddlReportType.SelectedIndex == 2)
            {
                txtReportDate2.Text = ddlYear2.Text;
                txtReportDate2.Text += ddlMonth2.Text;
                txtReportDate2.Text += ddlDay2.Text;
            }
            QuotesSummaryDB _quotessummarydb = new QuotesSummaryDB();
            string _InsuranceType = ddlInsuranceType.Text;
            Int16 _ReportType = Convert.ToInt16(ddlReportType.Text);
            string _ReportDate1 = txtReportDate1.Text.Trim();
            string _ReportDate2 = txtReportDate2.Text.Trim();
            string _RefCEUserId = Session[CE.Global.General.UserId].ToString();
            _dsSummary = _quotessummarydb.GetQuoteSummaryCount(_InsuranceType, _ReportType, _ReportDate1, _ReportDate2);
            if (_dsSummary != null && _dsSummary.Tables.Count > 0)
            {
                if (_InsuranceType == "CI")
                {
                    _dtCISummary = _dsSummary.Tables[0];

                    divCI.Visible = true;
                }
                if (_dtCISummary != null && _dtCISummary.Rows.Count > 0)
                {
                    lblError.Text = "";
                    lblCITotalCount.Text = "0";
                    lblCINewCount.Text = "0";
                    lblCIExistCount.Text = "0";
                    lblCICount.Visible = true;
                    DataView dvCI = new DataView((DataTable)_dtCISummary);
                    dvCI.Sort = ViewState["CISortExpression"].ToString() + ViewState["CISortOrder"].ToString();
                    grvCIQuotesSummary.DataSource = dvCI;
                    grvCIQuotesSummary.DataBind();
                    lblCIRecordCount.Text = grvCIQuotesSummary.Rows.Count.ToString() + " record(s).";
                }
                else
                {
                    grvCIQuotesSummary.DataSource = null;
                    grvCIQuotesSummary.DataBind();
                    lblCIRecordCount.Text = "";
                    lblCITotalCount.Text = "";
                    lblCINewCount.Text = ""; 
                    lblCIExistCount.Text = "";
                    lblCICount.Visible = false;
                }
            }
            else
            {
                grvCIQuotesSummary.DataSource = null;
                grvCIQuotesSummary.DataBind();

                lblCITotalCount.Text = "";
                lblCINewCount.Text = "";
                lblCIExistCount.Text = "";
                lblCICount.Visible = false;
                lblCIRecordCount.Text = "";
            }
        }
        else
        {
            grvCIQuotesSummary.DataSource = null;
            grvCIQuotesSummary.DataBind();

            lblCITotalCount.Text = "";
            lblCINewCount.Text = "";
            lblCIExistCount.Text = "";
            lblCICount.Visible = false;
            lblCIRecordCount.Text = "";

            divCI.Visible = false;
        }
    }

    public string GetReturningUserCount(string Total, string New)
    {
        string _ReturnUserCount = "0";
        _ReturnUserCount = (Convert.ToInt32(Total) - Convert.ToInt32(New)).ToString();
        return _ReturnUserCount;
    }

    private void CheckPageAccess()
    {
        Int64 PageAccess = General.GetPageAccess(Convert.ToInt64(Session[General.UserId].ToString()), PageCode);
        if (PageAccess == 0)
        {
            Response.Redirect("~/UserMessage.aspx?menu=0&mt=UM&ec=101");
        }
        //else if (PageAccess > 0)
        //{
        //    EnablePageAccess(PageAccess);
        //}
    }

    private void EnablePageAccess(Int64 PageAccess)
    {
        //if (PageAccess == 100)
        //{
        //    btnReject.Enabled = false;
        //}
        //else if (PageAccess == 300)
        //{
        //    btnReject.Enabled = true;
        //}
    }
    
}
